package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Category;
import com.ethan.cj.ssm.service.CategoryService;
import com.ethan.cj.ssm.service.ContentService;
import com.ethan.cj.ssm.util.PageBean;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


/**
 * 分类管理控制层
 */
@Controller
@Log4j
@RequestMapping("category")
public class CategoryController {
    /**
     * 分类管理service层映射
     */
    @Autowired
    private CategoryService cs;

    /**
     * 内容管理service层映射
     */
    @Autowired
    private ContentService cts;

    /**
     * 进入主页面
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "lw/categoryhome";
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "lw/insertcategory";
    }

    /**
     * 进入查看内容页面
     * @return
     */
    @RequestMapping("selcontent")
    public String selcontent(@RequestParam("caid") int caid, Model model) {
        model.addAttribute("caid",caid);
        return "lw/selcontent";
    }

    /**
     * 分页查询
     * @param page 页数
     * @param caname 查询条件 : 分类名称
     * @return
     */
    @RequestMapping("/categoryPage")
    @ResponseBody
    public Map<Object,Object> categoryPage(
            @RequestParam("page") int page
            , @RequestParam("limit") int limit
            , @RequestParam(value = "caname", required = false) String caname ) {
        PageBean<Category> pb = new PageBean<>(page,limit);
        return cs.selectCategory(pb ,caname);
    }

    /**
     * 添加一条分类管理信息
     *
     * @param caname 分类管理对象
     */
    @RequestMapping("/insertCategory")
    @ResponseBody
    public Map<Object,Object> inserCategory(@RequestParam("caname") String caname) {
        return cs.insertCategory(caname);
    }

    /**
     * 根据分类caid 查询内容信息
     *
     * @param caid 分类管理id
     */
    @RequestMapping("/selectContentByCaid")
    @ResponseBody
    public Map<Object, Object> selectContentByCaid(
            @RequestParam("page") int page
            , @RequestParam("limit") int limit
            , @RequestParam("caid") int caid) {
        return cts.selectContentByCaid(Integer.valueOf(caid));
    }

    /**
     * 根据id 删除分类管理信息
     *
     * @param caid 分类管理id
     */
    @RequestMapping("/deleteCategoryById")
    @ResponseBody
    public Map<Object,Object> deleteCategoryById(@RequestParam("caid") int caid) {
        return cs.deleteCategoryById(Integer.valueOf(caid));
    }
}
